<?php

namespace app\controller\student;

use app\BaseController;
use app\model\Course;
use app\model\VStudentCourse;
use app\model\StudentCourse as ModelStudentCourse;
use \think\facade\Db;

/**
 * 学生-课程控制器
 */
class StudentCourse extends BaseController
{
    /**
     * 课程列表
     */
    public function listMyCourse()
    {
        $courseList = VStudentCourse::where('SID', $this->user['SID'])->order('createTime','DESC')->select();
        return resultSuccess($courseList);
    }

    /**
     * 查找课程名
     */
    public function getCourseName()
    {
        $courseID = $this->request->param('courseID');
        $course = VStudentCourse::where(['SID' => $this->user['SID'], 'courseID' => $courseID])->find();
        $courseStudentNumber = ModelStudentCourse::where('courseID', $courseID)->count();
        $course['studentNumber'] = $courseStudentNumber;
        if ($course == null) {
            return resultError('无此课程');
        }
        return resultSuccess($course);
    }

    /**
     * 课程统计
     */
    public function courseCount()
    {
        $courseID = $this->request->param('courseID');
        $SID = $this->user['SID'];

        $mediaList = Db::query('SELECT
                                    media.MID,basicName,credit,SID 
                                FROM
                                    media
                                    LEFT JOIN studentmedia ON studentmedia.MID = media.MID AND SID = :SID
                                WHERE
                                    chapterID IN ( SELECT chapterID FROM chapter WHERE courseID = :courseID )',['SID'=>$SID,'courseID'=>$courseID]);

        $mediaTotal = 0;
        $studentMediaTotalCredit = 0;
        $studentMediaTotal = 0;
        foreach($mediaList as $media){
            $mediaTotal+=$media['credit'];
            if(!empty($media['SID'])){
                $studentMediaTotalCredit += $media['credit'];
                $studentMediaTotal++;
            }
        }

        $examList = Db::query('SELECT 
                                    exam.EID,title,exam.credit as examCredit, studentexam.credit as studentCredit,SID
                                FROM 
                                    exam
                                    LEFT JOIN studentexam on studentexam.EID = exam.EID AND SID = :SID
                                WHERE
                                    courseID = :courseID',['SID'=>$SID,'courseID'=>$courseID]);
        $examTotal = 0;
        $studentExamTotalCredit = 0;
        $studentExamTotal = 0;
        foreach($examList as $exam){
            $examTotal+=$exam['examCredit'];
            if(!empty($exam['SID'])){
                $studentExamTotalCredit += $exam['studentCredit'];
                $studentExamTotal ++;
            }
        }                            

        $discussList = Db::query('SELECT
                                        discuss.discussID , theme , credit, SID
                                    FROM
                                        discuss
                                        LEFT JOIN (SELECT DISTINCT(SID) AS SID,discussID FROM studentdiscuss) AS T1 ON T1.discussID = DISCUSS.discussID AND T1.SID = :SID
                                    WHERE courseID = :courseID',['SID'=>$SID,'courseID'=>$courseID]);
        $discussTotal = 0;
        $studentDiscussTotalCredit =0;
        $studentDiscussTotal = 0;
        foreach($discussList as $discuss){
            $discussTotal+=$discuss['credit'];
            if(!empty($discuss['SID'])){
                $studentDiscussTotalCredit += $discuss['credit'];
                $studentDiscussTotal ++;
            }
        }

        return resultSuccess(
            [
                'courseCount' => [ 
                    'mediaTotal' => $mediaTotal,
                    'examTotal' => $examTotal,
                    'discussTotal' => $discussTotal,
                ],
                'studentCount' =>[
                    'studentMediaTotalCredit' =>$studentMediaTotalCredit,
                    'studentExamTotalCredit' =>$studentExamTotalCredit,
                    'studentDiscussTotalCredit'=>$studentDiscussTotalCredit,
                    'studentMediaTotal' =>$studentMediaTotal,
                    'studentExamTotal' =>$studentExamTotal,
                    'studentDiscussTotal'=>$studentDiscussTotal,
                ],
                'mediaList'=>$mediaList , 
                'examList'=>$examList ,
                'discussList' => $discussList,
            ]);
    }

}
